/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 Umeng, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.umeng.comm.ui.fragments;

import android.content.Intent;
import android.location.Location;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qianxx.base.utils.SPUtil;
import com.umeng.comm.core.beans.CommUser;
import com.umeng.comm.core.impl.CommunitySDKImpl;
import com.umeng.comm.core.listeners.Listeners;
import com.umeng.comm.core.login.LoginListener;
import com.umeng.comm.core.sdkmanager.LocationSDKManager;
import com.umeng.comm.core.utils.CommonUtils;
import com.umeng.comm.core.utils.ResFinder;
import com.umeng.comm.core.utils.ToastMsg;
import com.umeng.comm.ui.activities.PostFeedActivity;
import com.umeng.comm.ui.imagepicker.presenter.impl.FeedListPresenter;
import com.umeng.comm.ui.imagepicker.presenter.impl.FriendFeedPresenter;
import com.umeng.comm.ui.imagepicker.presenter.impl.NearbyFeedPresenter;
import com.umeng.comm.ui.imagepicker.widgets.BaseView;
import com.umeng.community.example.Utils.ThirdLoginRequestUtils;

/**
 * 周边Feed的Fragment
 */
public class NearbyFeedFragment extends FriendsFragment {

    private View mLoginHolder;

    //未登录显示的文字
    private TextView mLoginTV;
    //
    private Button mLoginBtn;

//    public boolean isLocation = false;

    private BaseView mBaseView;

    public NearbyFeedFragment() {
    }

    @Override
    protected void initViews() {
        super.initViews();

        mBaseView = findViewById(ResFinder.getId("umeng_comm_baseview"));
        mLoginHolder = findViewById(ResFinder.getId("umeng_comm_login"));
        mLoginTV = findViewById(ResFinder.getId("umeng_comm_instrucment"));
        mLoginTV.setText("您登录后，服务器君才知道您附近的帖子哦～");
        findViewById(ResFinder.getId("umeng_comm_tologin")).setVisibility(View.GONE);
        findViewById(ResFinder.getId("umeng_comm_tologin")).setOnClickListener(

                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (TextUtils.isEmpty(SPUtil.getInstance().getToken()) || !CommonUtils.isLogin(getActivity())) {
                            CommunitySDKImpl.getInstance().login(getActivity(), new LoginListener() {
                                @Override
                                public void onStart() {

                                }

                                @Override
                                public void onComplete(int stCode, CommUser userInfo) {
                                    if (stCode == 0) {
                                        ThirdLoginRequestUtils.getInstance(getActivity()).ThirdLogin(userInfo);
                                        mLoginHolder.setVisibility(View.GONE);
                                    }
                                }
                            });
                        }else {
                            mLoginHolder.setVisibility(View.GONE);
                        }
                    }
                }

        );
        if (TextUtils.isEmpty(SPUtil.getInstance().getToken()) || !CommonUtils.isLogin(getActivity())) {
            mLoginHolder.setVisibility(View.VISIBLE);
//            mBaseView.setVisibility(View.GONE);
        }

    }

    @Override
    protected FriendFeedPresenter createPresenters() {
        return new NearbyFeedPresenter(this);
    }

    @Override
    protected void initWidgets() {
        super.initWidgets();
        RelativeLayout headerLay = (RelativeLayout) mRootView.findViewById(ResFinder
                .getId("umeng_comm_friend_id"));
        headerLay.setVisibility(View.GONE);
        TextView titleTextView = (TextView) mRootView.findViewById(ResFinder
                .getId("umeng_comm_title_tv"));
        titleTextView.setText(ResFinder.getString("umeng_comm_recommend_nearby"));
    }

    public static NearbyFeedFragment newNearbyFeedRecommend() {
        return new NearbyFeedFragment();
    }

    @Override
    protected void initAdapter() {
        super.initAdapter();
//        mFeedLvAdapter.setShowDistance();
    }


    /**
     * 主动调用加载数据。 【注意】该接口仅仅在退出登录时，跳转到FeedsActivity清理数据后重新刷新数据</br>
     */
    public void loadDataFromServer() {
        if (mPresenter != null) {
            mPresenter.loadFeedFromServer();
        }
    }


    @Override
    public void onUserLogout() {
        super.onUserLogout();
        if (mLoginHolder != null) {
            clearListView();
            mLoginHolder.setVisibility(View.VISIBLE);
//            mBaseView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onRefreshEnd() {
        super.onRefreshEnd();
        if (!isAdded()) {
            return;
        }
        if (TextUtils.isEmpty(SPUtil.getInstance().getToken()) || !CommonUtils.isLogin(getActivity())) {
            mLoginHolder.setVisibility(View.VISIBLE);
//            mBaseView.setVisibility(View.GONE);
        } else {
            mLoginHolder.setVisibility(View.GONE);
//            mBaseView.setVisibility(View.VISIBLE);
//            if (isLocation) {
//                isLocation = false;
//                mBaseView.setEmptyViewText("您的定位还没有打开");
//            }

        }
    }
}
